package com.metadata_system.system.response;

import com.metadata_system.common.db.entity.User;
import com.metadata_system.common.enums.AllConst;
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import javax.persistence.Column;
import java.util.*;

@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class UserResponse {
    private Integer id;

    /**
     * 姓名
     */
    private String name;

    /**
     * 头像
     */
    private String avatar;

    /**
     * 用户名
     */
    private String username;


    /**
     * 所属角色
     */
    private Object role;

    /**
     * 用户类型
     */
    private Object userType;


    /**
     * 状态: 0.关闭 1.启用
     */
    private Object status;

    /**
     * 附加参数
     */
    private Integer params;

    /**
     * 附加参数
     */
    private String code;

    private Object isLogicToolAccess;

    /**
     * 创建时间
     */
    @Column(name = "created_time")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss",timezone="GMT+8")
    private Date createdTime;

    public static UserResponse formatData(User user){
        UserResponse userResponse = UserResponse.builder()
                .id(user.getId())
                .name(user.getName())
                .username(user.getUsername())
                .avatar(user.getAvatar())
                .role(AllConst.IdName(user.getRole(), AllConst.UserRoleMap))
                .status(AllConst.IdName(user.getStatus(), AllConst.UserStatusList))
                .params(user.getParams())
                .createdTime(user.getCreatedTime())
                .isLogicToolAccess(AllConst.IdName(user.getIsLogicToolAccess(), AllConst.IsStatusArr))
                .build();
        return userResponse;
    }

    public static List<UserResponse> formatData(List<User> userList){
        List<UserResponse> userResponseList=new ArrayList<>();
        for (User user : userList) {
            userResponseList.add(UserResponse.formatData(user));
        }
        return userResponseList;
    }
}
